var map;
var pointArray = [];
var zoomfactor = {
    zoomFactor: -2 // 初始地图级别
}

var ALLmarker;
//加载地图
function NMapsShow(para) {
    para = $.extend({
        id: "allmap" //对象ID
    }, para);
    GMapsShow(para);
}

function GMapsShow(para) {
    map = new BMapGL.Map(para.id,
        {
            //minZoom: 9,
            maxZoom: 21,
            enableAutoResize: true,
            enableMapClick: true
        });

    map.centerAndZoom(new BMapGL.Point(118.162943,39.705555), 16);
    var navi3DCtrl = new BMapGL.NavigationControl3D();
    map.addControl(navi3DCtrl);
    //map.setMapStyleV2({
    //    styleId: '3cfd70688fc110a805a68d0876fe14a3'
    //});
    //map.setMapStyleV2({ styleJson: styleJson });
    map.setMapStyleV2({
        styleId: 'c264d750220c6474470d48024f7eaea8'
    });
    //map.setCurrentCity("唐山市");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    //    map.setCenter("唐山市");
    map.setHeading(45);             //设置地图旋转方向
    map.setTilt(55);                       //设置倾斜角度
    // map.setMapType(BMAP_EARTH_MAP);
    var scaleCtrl = new BMapGL.ScaleControl();  // 添加比例尺控件
    map.addControl(scaleCtrl);
    var zoomCtrl = new BMapGL.ZoomControl();  // 添加比例尺控件
    map.addControl(zoomCtrl);
    //单击获取点击的经纬度
//    map.addEventListener("click", function (e) {
 //       console.log(e.latlng.lng + "," + e.latlng.lat);
 //   });
    map.enableScrollWheelZoom(false);
    //定义一个控件类
    function ZoomControl() {
        this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
        this.defaultOffset = new BMapGL.Size(20, 20)
    }
    function addpoint(e) {
        console.log('weizhi' + e.latlng.lng + ',' + e.latlng.lat);
        var pt = new BMapGL.Point(e.latlng.lng, e.latlng.lat);
        var marker = new BMapGL.Marker(pt);
        map.addOverlay(marker);
    };

    //通过JavaScript的prototype属性继承于BMap.Control
    ZoomControl.prototype = new BMapGL.Control();
    ZoomControl.prototype.initialize = function (map) {
        map.enableScrollWheelZoom(false);
        var mydiv = document.createElement("div");
        var img_point = document.createElement("img");
        img_point.setAttribute("src", "../../../src/assets/bigdataStyle1/images/huadian.png");
        img_point.onclick = function () {
            map.addEventListener('click', addpoint );
        }

        var img_biao = document.createElement("img");
        img_biao.setAttribute("src", "../../../src/assets/bigdataStyle1/images/pingyi.png");
        img_biao.onclick = function () {
            map.removeEventListener('click', addpoint);
        }

        //mydiv.appendChild(img_biao);
        //mydiv.appendChild(img_point);
        //mydiv.appendChild(document.createElement("br"));
        map.getContainer().appendChild(mydiv);
        return mydiv;
    }
    //创建控件元素
    var myZoomCtrl = new ZoomControl();
    //添加到地图中
    map.addControl(myZoomCtrl);
}


function drawgraphic(points,title) {
    points = eval("(" + points + ")");
    var allpoints = [];
    for (var p = 0; p < points.length; p++) {
        var zu = points[p].POSITION.split("#");
        //  alert(zu.length + "  " + zu)
        for (var i = 0; i < zu.length; i++) {
            var dian = zu[i].split(";");
            //           alert(zu.length + "  " + dian + "  " + i + dian.length)
            if (dian.length == 1) {                          //点要素情况
                var x = parseFloat(dian[0].split(",")[0]);
                var y = parseFloat(dian[0].split(",")[1]);
                var coor = new BMapGL.Point(x, y);
                allpoints.push(coor);
                var myIcon = new BMapGL.Icon(points[p].IMG, new BMapGL.Size(29, 31),{anchor: new BMapGL.Size(15,30)});

                //ALLmarker = new BMapGL.Marker(coor, { icon: myIcon }, { title: points[p].NAME });
                ALLmarker = new BMapGL.Marker(coor, { title: points[p].NAME });


                map.addOverlay(ALLmarker);
                ALLmarker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                var opts1 = {
                    position: coor ,// 指定文本标注所在的地理位置
                    //offset: new BMapGL.Size(-20,5)
                }
                var clable = points[p].NAME;
                var label1 = new BMapGL.Label(clable, opts1); // 创建文本标注对象
                label1.setStyle({
                    color: "#ffffff",
                    fontSize: "14px",
                    height: "20px",
                    border: "0",
                    textAlign: "center",
                    fontFamily: "微软雅黑",
                    backgroundColor: "null",
                    transform: "translateX(-50%)"
                });
                map.addOverlay(label1);

                var content = points[p].HTML;
                var width = points[p].WIDTH;
                var height = points[p].HEIGHT;
                var type = points[p].TYPE ? points[p].TYPE : 2;
                addClickHandler(content, ALLmarker);

                function addClickHandler(content, ALLmarker) {
                    ALLmarker.addEventListener("click", function (e) {
                        /*layer.open({
                            type: 2,
                            skin: 'layer-modal', //样式类名
                            area: ['900px', '430px'],
                            // area: [width, height],
                            title: title,
                            anim: 2,
                            move: false,
                            shadeClose: true, //开启遮罩关闭
                            content: content,
                            btn: ['确定']
                        });*/
                        layer.open({
                            type: 2,
                            skin: 'layui-layer-rim', //加上边框
                            area: ['800px', '500px'], //宽高
                            content: 'http://60.2.176.70:9281/devHis/sige2',
                            btn: ['切换', '关闭']
                            ,yes: function(index){
                            layer.close(index);
                            // 地图vr切换
                            $("#vr").attr("src","http://60.2.176.70:9282");
                            $("#allmap").removeClass("show").addClass("hide");
                            $(".vrcon").removeClass("hide").addClass("show").addClass("animate__flipInY");
                        }
                        });
                    });
                }
            }
        }
    }
   // map.setViewport(allpoints);
}


